using UnityEngine;

public class bulletmove : BulletController
{
	public LayerMask layerMask;

	private Vector3 previousPosition;

	private Vector3 movedir;

	private float movedis;

	private RaycastHit hitInfo;

	private float minCheckDis = 1f;

	private AIController curhitai;

	private Fri_AIController fri_curhitai;

	private Effectobj tempeffectobj;

	private void Update()
	{
		if (!isshot)
		{
			return;
		}
		m_transform.Translate(0f, 0f, movespeed * Time.deltaTime, Space.Self);
		shotouttime += Time.deltaTime;
		if (shotouttime >= (float)limitedtime)
		{
			isshot = false;
			shotouttime = 0f;
			targetAI = null;
			if (isplayerbullet)
			{
				bulletpool.instance.RecycleBullet(this);
			}
			else
			{
				NPCbulletpool.instance.RecycleBullet(this);
			}
		}
	}

	public void shot(int buldam, int penetraVal, int parentnum, bool isfri = false)
	{
		m_transform = base.transform;
		parent = parentnum;
		previousPosition = m_transform.position;
		hitDamage = buldam;
		penetrationVal = penetraVal;
		if (targetAI != null && isplayerbullet)
		{
			endpos = targetAI.CalcAim();
			m_transform.LookAt(endpos);
		}
		if (!isplayerbullet || isfri)
		{
			m_transform.LookAt(endpos);
		}
		isshot = true;
		shotouttime = 0f;
		UnityVersionUtil.SetActiveRecursive(m_transform.gameObject, state: true);
	}

	private void FixedUpdate()
	{
		if (!isshot)
		{
			return;
		}
		movedis = Vector3.Distance(previousPosition, m_transform.position);
		if (!(movedis >= minCheckDis))
		{
			return;
		}
		movedir = (m_transform.position - previousPosition).normalized;
		if (Physics.Raycast(previousPosition, movedir, out hitInfo, movedis, layerMask.value))
		{
			tempeffectobj = null;
			if (isplayerbullet)
			{
				switch (hitInfo.collider.gameObject.tag)
				{
				case "water":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletwater);
					}
					break;
				case "Terrain":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					break;
				case "AIPlane":
					curhitai = hitInfo.transform.GetComponent<AI_plane>();
					if (curhitai != null)
					{
						curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "building":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					break;
				case "ship":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					curhitai = hitInfo.transform.parent.GetComponent<ShipController>();
					if (curhitai != null)
					{
						curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "buildingai":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					curhitai = hitInfo.transform.parent.GetComponent<BuildingAIController>();
					if (curhitai != null)
					{
						curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				}
				if (tempeffectobj != null)
				{
					tempeffectobj.transform.position = hitInfo.point;
					tempeffectobj.showeffect();
				}
				isshot = false;
				shotouttime = 0f;
				targetAI = null;
				bulletpool.instance.RecycleBullet(this);
			}
			else
			{
				switch (hitInfo.collider.gameObject.tag)
				{
				case "water":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletwater);
					}
					break;
				case "Terrain":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					break;
				case "Player":
					plane_smothfollow.instance.getdamage(hitDamage, penetrationVal, parent, isbullet: true);
					break;
				case "building":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					break;
				case "ship":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					break;
				case "friplane":
					fri_curhitai = hitInfo.transform.GetComponent<Fri_plane>();
					if (fri_curhitai != null)
					{
						fri_curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "friship":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					fri_curhitai = hitInfo.transform.parent.GetComponent<Fri_ship>();
					if (fri_curhitai != null)
					{
						fri_curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "fribuilding":
					if (InviewPos(hitInfo.point))
					{
						tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.bulletterrain);
					}
					fri_curhitai = hitInfo.transform.parent.GetComponent<Fri_building>();
					if (fri_curhitai != null)
					{
						fri_curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "AIPlane":
					if (GlobalInf.isInternalInjury)
					{
						curhitai = hitInfo.transform.GetComponent<AI_plane>();
						if (curhitai != null)
						{
							curhitai.GetDamage(hitDamage, penetrationVal, parent);
						}
					}
					break;
				}
				if (tempeffectobj != null)
				{
					tempeffectobj.transform.position = hitInfo.point;
					tempeffectobj.showeffect();
				}
				isshot = false;
				shotouttime = 0f;
				targetAI = null;
				NPCbulletpool.instance.RecycleBullet(this);
			}
		}
		previousPosition = m_transform.position;
	}
}
